package juc.thread.secuity;

/*
 * wait notify 的正确使用方式
 * */
public class Test8 {
    static boolean money = false;
    static boolean yan = false;
    public static void main(String[] args) {
        final Object lock = new Object();
        new Thread(() -> {
            synchronized (lock) {
                while (!money) {
                    try {
                        System.out.println("没加班费不干...");
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("00后开始干活...");
            }
        }, "00后").start();
        new Thread(() -> {
            synchronized (lock) {
                while (!yan) {
                    try {
                        System.out.println("没烟不干...");
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("90后开始干活...");
            }
        }, "90后").start();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                synchronized (lock) {
                    System.out.println("普?开始干活");
                }
            }, "普" + i).start();
        }
        synchronized (lock) {
            System.out.println("给加班费...也给烟");
            money = true;
            yan=true;
//            lock.notify();
            lock.notifyAll();
        }
    }
}
